/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.utils.MybatisUtils;
import com.jnzc.ocr.serv.shop.entity.ShopSupplierCategory;
import com.jnzc.ocr.serv.shop.entity.ShopSupplierCategory;
import com.jnzc.ocr.serv.shop.filter.ShopSupplierCategoryFilter;
import com.jnzc.ocr.serv.shop.filter.ShopSupplierCategoryFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopSupplierCategoryMapper;
import com.jnzc.ocr.serv.shop.service.ShopSupplierCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Service
public class ShopSupplierCategoryServiceImpl implements ShopSupplierCategoryService {

    @Autowired
    private ShopSupplierCategoryMapper shopSupplierCategoryMapper;

    @Override
    public ShopSupplierCategory find(Long id, Long shopId) {
        ValidateUtils.isId(id, "会员分类ID");
        ShopSupplierCategory filter = new ShopSupplierCategory();
        filter.setId(id);
        filter.setShopId(shopId);
        ShopSupplierCategory obj = shopSupplierCategoryMapper.selectOne(filter);
        BusinessUtils.notNull(obj, "会员分类信息为空");
        return obj;
    }

    @Override
    public List<ShopSupplierCategory> list(ShopSupplierCategoryFilter filter) {
        EntityWrapper<ShopSupplierCategory> ew = createEntityWrapper(filter);
        return shopSupplierCategoryMapper.selectList(ew);
    }

    @Override
    public PageResult<ShopSupplierCategory> search(ShopSupplierCategoryFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo, pageSize);
        EntityWrapper<ShopSupplierCategory> ew = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(shopSupplierCategoryMapper.selectPage(pagination, ew), pagination);
    }

    @Override
    public ShopSupplierCategory save(ShopSupplierCategory obj) {
        if(null == obj.getId()){
            obj.setCreateTime(DateUtils.getNow());
            shopSupplierCategoryMapper.insert(obj);
        } else {
            shopSupplierCategoryMapper.updateById(obj);
        }
        return obj;
    }

    @Override
    public void delete(Long id) {
        shopSupplierCategoryMapper.deleteById(id);
    }

    private EntityWrapper<ShopSupplierCategory> createEntityWrapper(ShopSupplierCategoryFilter filter) {
        ShopSupplierCategory entity = new ShopSupplierCategory();
        entity.setShopId(filter.getShopId());
        EntityWrapper<ShopSupplierCategory> ew = new EntityWrapper<ShopSupplierCategory>(entity);
        MybatisUtils.fillEntityWrapper(ew, filter);
        return ew;
    }
}
